﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using QLTT.Models;
using QLTT.Properties;
using QLTT.Commands;
using System.Text.RegularExpressions;

namespace QLTT.ViewModels
{
    class TeacherViewModel : WorkspaceViewModel, System.ComponentModel.IDataErrorInfo
    {
        private Teacher _teacher;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _editCommand;
        private DelegateCommand _cancelCommand;
                
        private bool _isSelected;
        public TeacherViewModel(icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _teacher = new Teacher();
            base.DisplayName = Properties.Resources.Base_DisplayName_Teacher_New;
        }
        public TeacherViewModel(icanDataContext dataContext, Teacher teacher)
        {
            _teacher = teacher;
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_Teacher_Edit;
        }
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(Edit);
                }
                return _editCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }
        
        private void Save()
        {
            if (_dataContext == null || _teacher == null)
                return;
            string[] properties = {"Name", "Email", "Address", "PhoneNumber", "Salary"};
            if (!ValidateData(properties))
                return;
            try
            {
                bool isInsert = _teacher.ID == 0;
                _teacher.LastUpdated = DateTime.Now;
                if (isInsert)
                {
                    _dataContext.Teachers.InsertOnSubmit(_teacher);
                }
                _dataContext.SubmitChanges();
                if (isInsert)
                    Mediator.NotifyColleagues<Teacher>(Messages.ListTeacherUpdated, _teacher);
                Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
            }
            catch (Exception ex)
            {
                this.ErrorMsg = Properties.Resources.ErrorMessage_General;
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
            
            
        }
       
        private void Edit()
        {
            if (_dataContext == null || _teacher == null)
                return;
            
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, this);
        }
        private void Cancel()
        {
            if(_teacher.ID > 0)
                _dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _teacher);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public void Delete()
        {
            if (_dataContext == null || _teacher == null)
                return;
            try
            {
                _dataContext.Teachers.DeleteOnSubmit(_teacher);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
        public bool CanDelete
        {
            get { return _teacher.Schedules.Count == 0; }
        }
        public long ID
        {
            get { return _teacher.ID; }
        }

        public string Name
        {
            get { return _teacher.Name; }
            set
            {
                if (_teacher.Name == value) return;
                _teacher.Name = value;
                base.OnPropertyChanged("Name");
            }
        }
        public string Email
        {
            get { return _teacher.Email; }
            set
            {
                if (_teacher.Email == value) return;
                _teacher.Email = value;
                base.OnPropertyChanged("Email");
            }
        }
        public string Address
        {
            get { return _teacher.Address; }
            set
            {
                if (_teacher.Address == value) return;
                _teacher.Address = value;
                base.OnPropertyChanged("Address");
            }
        }
        public string PhoneNumber
        {
            get { return _teacher.PhoneNumber; }
            set
            {
                if (_teacher.PhoneNumber == value) return;
                _teacher.PhoneNumber = value;
                base.OnPropertyChanged("PhoneNumber");
            }
        }
        public System.Nullable<double> Salary
        {
            get { return _teacher.Salary; }
            set
            {
                if (_teacher.Salary == value) return;
                _teacher.Salary = value;
                base.OnPropertyChanged("Salary");
            }
        }
        public List<Schedule> Schedules
        {
            get { return _teacher.Schedules.ToList(); }
            
        }
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                Regex rxValidate;
                switch (property)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(this.Name))
                            msg = Resources.ValidationTeacherName_Error;
                        break;
                    case "PhoneNumber":
                        if(string.IsNullOrEmpty(this.PhoneNumber))
                            msg = Resources.ValidationTeacherPhoneNumber_Error;
                        break;
                    case "Email":
                        rxValidate = new Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", RegexOptions.Singleline);
                        if (string.IsNullOrEmpty(this.Email) || !rxValidate.Match(this.Email).Success)
                            msg = Resources.ValidationTeacherEmail_Error;
                        break;
                    case "Salary":
                        if(this.Salary == null || this.Salary <= 0)
                            msg = Resources.ValidationTeacherSalary_Error;
                        break;
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members

    }
}
